package com.ruoyi.web.controller.system.web;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.UtilTool;
import com.ruoyi.system.domain.WebSite;
import com.ruoyi.system.mapper.WebSiteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.WebCate;
import com.ruoyi.system.service.IWebCateService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 分类Controller
 * 
 * @author ruoyi
 * @date 2021-07-13
 */
@Controller
@RequestMapping("/system/cate")
public class WebCateController extends BaseController
{
    private String prefix = "system/cate";

    @Autowired
    private IWebCateService webCateService;
    @Autowired
    private WebSiteMapper webSiteMapper;

    /**
     * 查询分类列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WebCate webCate)
    {
        startPage();
        List<WebCate> list = webCateService.selectWebCateList(webCate);
        return getDataTable(list);
    }

    /**
     * 导出分类列表
     */
    @Log(title = "分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WebCate webCate)
    {
        List<WebCate> list = webCateService.selectWebCateList(webCate);
        ExcelUtil<WebCate> util = new ExcelUtil<WebCate>(WebCate.class);
        return util.exportExcel(list, "分类数据");
    }


    /**
     * 新增保存分类
     */
    @Log(title = "分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WebCate webCate)
    {
        webCate.setSource(0);
        webCate.setCreateTime(new Date(System.currentTimeMillis()));
        webCate.setUpdateTime(new Date(System.currentTimeMillis()));
        return toAjax(webCateService.insertWebCate(webCate));
    }



    /**
     * 修改保存分类
     */
    @Log(title = "分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WebCate webCate)
    {
        webCate.setSource(0);
        webCate.setUpdateTime(new Date(System.currentTimeMillis()));
        return toAjax(webCateService.updateWebCate(webCate));
    }

    /**
     * 删除分类
     */
    @Log(title = "分类", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        int size = 0;
        String[] id = ids.split(",");
        logger.info("长度为："+id.length);
        for (int i = 0; i < id.length; i++){
            WebCate webCate = webCateService.selectWebCateById(Long.valueOf(id[i]));
            if (!UtilTool.isNull(webCate)){
                WebSite webSite = new WebSite();
                webSite.setCateId(webCate.getId());
                List<WebSite> webSites = webSiteMapper.selectWebSiteList(webSite);
                if (webSites.size() > 0){
                    size= size+1;
                }
            }
        }
        if (size>0){
            return AjaxResult.error("删除失败，该分类下有网址信息");
        }else {
            return toAjax(webCateService.deleteWebCateByIds(ids));
        }
    }
}
